<?php
final class Controller_Album_Picture extends Controller_Security{
    protected function Acquiescent(){
        $id=Route::GetArguments('album');
        $data=Database::GetRows('select * from app_album_picture where Album=? order by ID desc',array($id));
        Template::Assign('Data',$data);
        Template::Display('album/picture/list.html');
    }

    protected function Add(){
        $action=Route::GetArguments('action');
        if('save'===$action){
            $filecount=$_POST['count'];
            for($i=1;$i<$filecount;$i++){
                $picture=$_POST['file'.$i];
                $temp=explode('/',$picture);
                $temp[count($temp)-1]= 't_'.$temp[count($temp)-1];
                $thumbnail=implode('/',$temp);
                Database::Insert('app_album_picture',array(
                    'Remark'=>$_POST['remark'.$i],
                    'Picture'=>$picture,
                    'Thumbnail'=>$thumbnail,
                    'Album'=>$_POST['album']
                ));
            }
            header('location:./?application=admin&controller=album_picture&album='.$_POST['album']);
        }
        else{
            Template::Display('album/picture/add.html');
        }
    }

    protected function Delete(){
        $id=Route::GetArguments('id');
        Database::DeleteRowsByValue('app_album_picture','ID',$id);
        header('location:'.$_SERVER['HTTP_REFERER']);
    }

    protected function Upload(){
        list($filename,$error)=array(NULL,NULL);
        $up=new Uploader('file');
        $up->Size=20481212300*12;
        $up->Path=$GLOBALS['ApplicationConfig']['Path']['UploadPicture'];
        $up->FileExtension=array('gif','jpg','png','bmp');
        $up->Execute();
        switch($up->ErrorCode){
            case Uploader::UPLOAD_ERR_OK:
                $filename=$up->Filename;
            break;
            case Uploader::UPLOAD_ERR_NO_FILE:
                $error='请上传文件。';
            break;
            case Uploader::UPLOAD_ERR_EXTENSION:
                $error='扩展名不正确。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                $error='文件大小不正确。';
            break;
            default:
                //$error=$up->ErrorCode;
                $error='上传失败';
            break;
        }
        //生成缩略图
        $ic=new Image($GLOBALS['ApplicationConfig']['Path']['UploadPicture'].'/'.$filename,$GLOBALS['ApplicationConfig']['Path']['UploadPicture'].'/t_'.$filename);
        $ic->Crop(100,100,2);
        $ic->SaveImage();
        $ic->destory();
        //渲染模板
        Template::Assign('Filename',$GLOBALS['ApplicationConfig']['Path']['URIPicture'].'/'.$filename);
        Template::Assign('Error',$error);
        Template::Display('upload/callback.html');
    }
}
?>